package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.adapter.BasePageAdapter;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeFreePreviewActivity extends BaseCodeFinishActivity {


        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_set)
        Button codeIvSet;

        @BindView(R.id.code_bg_vp)
        ViewPager codeBgVp;

        private ToastTools toastTools;

        private int currentPosition;

        private List<String> imagePaths;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_free_preview);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                initData();
        }

        private void initData() {
                final CodeMarketingInfo marketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                imagePaths = marketingInfo.getImagePaths();
                codeBgVp.setAdapter(new BasePageAdapter(R.layout.code_bg_item_iv, imagePaths.size()) {
                        @Override
                        public void setPageView(View inflate, int position, int layoutID, Object o) {
                                ImageView imageView = (ImageView) inflate.findViewById(R.id.imageView);
                                GlideApp.with(CodeFreePreviewActivity.this).clear(imageView);
                                GlideApp.with(CodeFreePreviewActivity.this)
                                        .asDrawable()
                                        .load(IpMsgConst.HTTP_URL_CHEST + imagePaths.get(position).replace("/uupic/","/uupic_thumb/"))
                                        .thumbnail(0.1f)
                                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                        .skipMemoryCache(false)
                                        .placeholder(R.drawable.sales_banner01)//占位图
                                        .error(R.drawable.sales_banner01)
                                        .into(imageView);
                        }
                });
                codeBgVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                                currentPosition = position;
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                });

                codeBgVp.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                switch (event.getAction()) {
                                        case MotionEvent.ACTION_DOWN:
                                                codeBgVp.removeCallbacks(runnable);
                                                break;
                                        case MotionEvent.ACTION_UP:
                                        case MotionEvent.ACTION_CANCEL:
                                                codeBgVp.removeCallbacks(runnable);
                                                codeBgVp.postDelayed(runnable, 10 * 1000);
                                                break;
                                }
                                return false;
                        }
                });
                codeBgVp.removeCallbacks(runnable);
                codeBgVp.postDelayed(runnable, 10 * 1000);
        }

        private Runnable runnable = new Runnable() {
                @Override
                public void run() {
                        currentPosition++;
                        if (currentPosition >= imagePaths.size()) {
                                currentPosition = 0;
                        }
                        codeBgVp.setCurrentItem(currentPosition, true);
                        codeBgVp.postDelayed(this, 10 * 1000);
                }
        };

        @OnClick({R.id.code_iv_back, R.id.code_iv_set})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_set:
                                upMarketingData();
                                break;
                }
        }
        private Dialog dialogLoading;
        private void upMarketingData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                CodeMarketingInfo info = CodeDataHelper.getInstance().getCodeMarketingInfo();
                String url = IpMsgConst.HTTP_URL_CHEST + "codeMarketingAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 0);
                hashMap.put("data", JSON.toJSONString(info));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                CodeDataHelper.getInstance().saveData();
                                sendFinishCodeBroadcast();
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.service_error_hint));
                        }
                });
        }


        @Override
        protected void onDestroy() {
                super.onDestroy();
                codeBgVp.removeCallbacks(runnable);
        }
}
